package org.zsqx.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

public class UploadTools {
	Logger log = Logger.getLogger(this.getClass());
	public String upload(String root,File pic,String picFileName,String serverfilename) throws Exception{
		if (null != pic) {
			File destFile = new File(root, serverfilename);
			InputStream is = new FileInputStream(pic);
			OutputStream os = new FileOutputStream(destFile);
			byte[] buffer = new byte[400];
			int length = 0;

			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}
			pic.delete();
			is.close();
			log.debug("上传成功");
			os.close();
			return serverfilename;
		} else {
			log.error("上传文件文件空");
			return null;
		}
	}
	
	public String upload(String root,File pic,String picFileName) throws Exception{
		SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String ext = picFileName.substring(picFileName.lastIndexOf("."),
				picFileName.length());
		String serverfilename = sim.format(new Date()) + ext;
		log.debug("扩展名是：" + ext);
		return upload(root, pic, picFileName, serverfilename);
	}

}
